/***************************************
 ***                                 ***
 ***                                 ***
 ***                                 ***
 ***************************************/
 
/** InputForm [静态类页面 获取 / 渲染 数据] */
let InputForm = (function(){
    // 类的内部方法
    var PrivateClass = {
        /**
         * [getControl 控件选择器 (页面渲染主控件)]
         * @Author   SEven
         * @DateTime 2019-01-31
         */
        getControl: function (MainCtrlName ,ParentCtrl = false){
            ParentCtrl = ParentCtrl === false ? document : ParentCtrl;
            
            let MainCtrl = ParentCtrl.getElementsByClassName(MainCtrlName);
            MainCtrl = MainCtrl === null ? ParentCtrl.getElementById(MainCtrlName ,true) : MainCtrl;

            
            if(MainCtrl instanceof HTMLCollection){
                MainCtrl = MainCtrl[0] === undefined ? false : MainCtrl[0];
            }
            return MainCtrl;
        },
        RenderingGroup: function (MainCtrlName ,MainData ,overflow ,ParentCtrl = false){
            let MainCtrl = PrivateClass.getControl(MainCtrlName ,ParentCtrl);
            
            let DemoCtrls = false;
            if(MainCtrl.children.length > 0)
                DemoCtrls = MainCtrl.children[0].cloneNode(true);

            for(var i=0;i < MainData.length;i++){
                PrivateClass.Rendering(MainCtrlName ,DemoCtrls ,MainData[i] ,overflow);
                overflow = false;
            }
        },
        /** [Rendering 渲染 Element Value] */
        Rendering: function (MainCtrlName ,DemoCtrls ,MainData ,overflow){
            // 获取渲染主控件
            let MainCtrl = PrivateClass.getControl(MainCtrlName);
            let ElemGroup = DemoCtrls === false ? MainCtrl : DemoCtrls.cloneNode(true);
            
            // 获取控件元素集
            let CtrlElems = ElemGroup.querySelectorAll('div,span,p,input,img,select');
            for(var i=0;i < CtrlElems.length;i++){
                let inputElem = CtrlElems[i];
                let key = inputElem.getAttribute('name');

                // 重新获取最新的数据
                let subGroup = inputElem.getAttribute('class') ? inputElem.getAttribute('class') : "";
                let subGroupKey = MainData[subGroup] ? subGroup : (MainData[subGroup.replace(/-/g ,'_')] ? subGroup.replace(/-/g ,'_') : false);
                if(subGroupKey !== false && MainData[subGroupKey] instanceof Array) {
                    // 列入子项列表
                    PrivateClass.RenderingGroup(subGroup ,MainData[subGroupKey] ,true ,ElemGroup);
                }

                key = key !== null ? (MainData[key] ? key : (MainData[key.replace(/-/g ,'_')] ? key.replace(/-/g ,'_') : false)) : false;
                if(key !== false){
                    // 选择显示控件 [ 仅控件显示 / 控件值替换显示 ]
                    let isExists = inputElem.getAttribute('is-exists');
                    let isExistsVal = inputElem.getAttribute('is-exists-val');
                    // 控件类型
                    let TagName = inputElem.tagName;
                    switch(TagName){
                        case 'DIV':
                        case 'SPAN':
                        case 'P':
                            if(isExists && isExists != MainData[key]) inputElem.style.display = 'none';
                            if(isExistsVal && isExistsVal != MainData[key]) inputElem.style.display = 'none';
                            if(!isExists || isExistsVal == MainData[key]) inputElem.innerHTML = MainData[key];
                            break;
                        case 'INPUT':
                        case 'SELECT':
                            if(isExists && isExists != MainData[key]) inputElem.style.display = 'none';
                            if(isExistsVal && isExistsVal != MainData[key]) inputElem.style.display = 'none';
                            if(!isExists || isExistsVal == MainData[key]) inputElem.value = MainData[key];
                            break;
                        case 'IMG':
                            if(isExists && isExists != MainData[key]) inputElem.style.display = 'none';
                            if(isExistsVal && isExistsVal != MainData[key]) inputElem.style.display = 'none';
                            if(!isExists || isExistsVal == MainData[key]) inputElem.src = MainData[key];
                            break;
                    }
                }
            }
            overflow ? MainCtrl.innerHTML = "" : "";
            DemoCtrls === false ? "" : MainCtrl.appendChild(ElemGroup);
        }
    };

    var ProtectedClass = {
        description: 'This Method For Protected Method For Class',
         /** [setElem 页面单独渲染] */
        setElem: function (MainCtrlName ,MainData){
            PrivateClass.Rendering(MainCtrlName ,false ,MainData ,true);
        },
        /** [setGroup 页面循环渲染] */
        setGroup: function (MainCtrlName ,MainData ,overflow = true){
            PrivateClass.RenderingGroup(MainCtrlName ,MainData ,overflow);
        }
    };

    // 类的内部方法
    var StaticClass = {
        description: 'This Method For Get Or Set The Input Value',
        /** [set 设置目标下的所有元素的值] */
        set: function (MainCtrlName ,MainData ,overflow = true){
            return MainData instanceof Array ?
                        ProtectedClass.setGroup(MainCtrlName ,MainData ,overflow) :
                        ProtectedClass.setElem(MainCtrlName ,MainData ,overflow);
        }
    };



    return StaticClass;
})();